package com.itheima.utils;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.itheima.enums.MsgCode;

import java.util.Map;

/**
 * @Description: 响应数据工具类
 * @Date :2019-08-15 15:06
 * @Author : yuan_he_song
 */
public class ResultUtils {
    public static final String CURRENT_USER = "currentUser";
    /**
     *  成功返回
     */
    public static JSONObject successData(Object obj, String msg){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", MsgCode.SUCCESS.getCode());
        jsonObject.put("msg",msg);
        jsonObject.put("data",obj);
        return jsonObject;
    }
    /**
     *  成功返回--->分页数据
     */
    public static JSONObject successAndPageData(Object obj, PageInfo pageInfo, String msg){
//        PageInfoCommon pageInfoCommon = new PageInfoCommon();
//        BeanUtils.copyProperties(pageInfo,pageInfoCommon);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", MsgCode.SUCCESS.getCode());
        jsonObject.put("success",MsgCode.SUCCESS.getSuccess());
        Map<String,Object> result = Maps.newHashMap();
        result.put("pageNumber",pageInfo.getPageNum()); // 当前页
        result.put("objectsPerPage",pageInfo.getPageSize()); // 每页记录数
        result.put("total",pageInfo.getTotal());
        result.put("pages",pageInfo.getPages());
        result.put("list",obj);
        jsonObject.put("data",result);
//        jsonObject.put("pageInfo",pageInfoCommon);
        return jsonObject;
    }


    public static JSONObject failureData(MsgCode msgCode) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg", msgCode.getMsg());
        jsonObject.put("code", msgCode.getCode());
        return jsonObject;
    }
    public static JSONObject failureMsg(String msge) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg",msge);
        jsonObject.put("code",-2);
        return jsonObject;
    }

}
